﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Sohar.Core
{
	public class WordClass
	{
		private static WordClass[] cache;

		public WordClass(int id, string name, string abbreviation)
		{
			this.Name = name;
			this.Abbreviation = abbreviation;
			this.ID = id;
		}

		public string Name
		{
			get;
			set;
		}

		public string Abbreviation
		{
			get;
			set;
		}

		public int ID
		{
			get;
			set;
		}

		private static WordClass[] Cache
		{
			get
			{
				if (cache == null)
				{
					List<WordClass> classes = new List<WordClass>();
					SoharGateway gateway = new SoharGateway();
					using (IDataReader reader = gateway.GetAllClasses())
					{
						while (reader.Read())
						{
							classes.Add(new WordClass(
								(int)reader["ID"],
								(string)reader["Name"],
								(string)reader["Abbreviation"]));
						}
					}

					cache = classes.ToArray();
				}

				return cache;
			}
		}

		public static WordClass GetByID(int id)
		{
			return (from c in Cache
					where c.ID == id
					select c).First();
		}

		public static WordClass GetByName(string @class)
		{
			return (from c in Cache
					where c.Name.Equals(@class, StringComparison.InvariantCultureIgnoreCase)
					select c).First();
		}
	}
}
